
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tompai.datagather.common.MybatisUtil;
import com.tompai.datagather.dao.TwitterAcountSeedsMapper;
import com.tompai.datagather.dao.TwitterAcountsMapper;
import com.tompai.datagather.model.TwitterAcountSeeds;
import com.tompai.datagather.model.TwitterAcounts;
import com.tompai.datagather.shedule.TwitterFollowClient;
import com.tompai.datagather.shedule.TwitterHomeClient;

/**
 * @desc: datagather-tools
 * @name: TwitterMulThreadsCrawlerDemo.java
 * @author: tompai
 * @createTime: 2018年5月17日 下午2:06:20
 * @history:
 * @version: v1.0
 */

public class TwitterMulThreadsCrawlerDemo {

	public static final Logger LOG = LoggerFactory.getLogger(TwitterMulThreadsCrawlerDemo.class);

	/**
	 * @author: tompai
	 * @createTime: 2018年5月17日 下午2:06:20
	 * @history:
	 * @param args
	 *            void
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int index = 14;// 597882//
		String seed = null;
		String roleA = "following";
		String roleB = "followers";

		TwitterAcounts acounts = new TwitterAcounts();
		TwitterAcountsMapper acountsMapper = MybatisUtil.getSession().getMapper(TwitterAcountsMapper.class);

		TwitterAcountSeeds seeds = new TwitterAcountSeeds();
		TwitterAcountSeedsMapper seedsMapper = MybatisUtil.getSession().getMapper(TwitterAcountSeedsMapper.class);
		// ================
		int nThreads = 4;
		ExecutorService pools = Executors.newFixedThreadPool(nThreads);
		/////
		do {
			// get the seed account//
			index++;
			seeds = seedsMapper.selectByPrimaryKey(Long.valueOf(index));
			if (seeds != null) {
				String acount = seeds.getAcount();
				if (StringUtils.isEmpty(acount)) {
					continue;
				}
				LOG.info("Index:" + index + " - " + " Current acount: " + acount);
				acounts = acountsMapper.selectByScreenName(acount);
				if (acounts != null) {
					LOG.info("Index:" + index + " - Have visited: " + acounts.getProfileScreenname());
				} else {
					seed = seeds.getAcount();
					if (StringUtils.isEmpty(seed)) {
						continue;
					}
					LOG.info("Index:" + index + " - " + " Begain twitter acount: " + seed);
					// twitterHomeInfo.parseHomePage(seed);
					pools.execute(new TwitterHomeClient(seed));
					LOG.info("Index:" + index + " - " + "Have add twitter account " + seed + " to the visited list.");
					// =============//
					LOG.info("Index:" + index + " - Begain search the twitter follow infomation: " + seed);
					// followPage.parseFollow(seed, roleA);
					// followPage.parseFollow(seed, roleB);
					pools.execute(new TwitterFollowClient(seed, roleA));
					pools.execute(new TwitterFollowClient(seed, roleB));
					try {
						boolean status = false;
						pools.shutdown();
						while (!status) {
							status = pools.isTerminated();
							Thread.sleep(3 * 1000);
						}
						LOG.info("END the loop acount: "+seed);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else {
				LOG.info("The Seeds list is Empty,Please add a acount in the seeds list ,then begainning.");
				break;
			}
		} while (true);
		pools.shutdown();
	}

}
